package com.yry.security;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Security;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.MD2Digest;
import org.bouncycastle.crypto.digests.MD4Digest;
import org.bouncycastle.crypto.digests.MD5Digest;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

/**
 * 1、单向加密：消息摘要算法加密
 * JDK:MD2/MD5
 * BC:MD2/MD4/MD5
 * CC:MD2/MD5
 * @author Administrator
 *
 */
public class MDUtils {
	/**
	 * JDK实现MD2
	 * @param srcStr
	 * @return
	 */
	public static String jdkMd2(String srcStr){
		String md5Str = null;
		try {
			MessageDigest md = MessageDigest.getInstance("MD2");
			byte[] md5Bytes = md.digest(srcStr.getBytes());
			
			md5Str = Hex.encodeHexString(md5Bytes);//将byte数组转成16进制；
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return md5Str;
		}
		return md5Str;
	}	
	
	/**
	 * JDK实现MD5
	 * @param srcStr
	 * @return
	 */
	public static String jdkMd5(String srcStr){
		String md5Str = null;
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] md5Bytes = md.digest(srcStr.getBytes());
			
			md5Str = Hex.encodeHexString(md5Bytes);//将byte数组转成16进制；
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return md5Str;
		}
		return md5Str;
		
	}
	/**
	 * BC,md2 
	 * @param srcStr
	 * @return
	 */
	public static String bcMd2(String srcStr){
		Digest digest = new MD2Digest();
		digest.update(srcStr.getBytes(), 0, srcStr.length());
		byte[] md2Bytes = new byte[digest.getDigestSize()];
		digest.doFinal(md2Bytes, 0);
		String md2Str = org.bouncycastle.util.encoders.Hex.toHexString(md2Bytes);
		return md2Str;
		
	}
	/**
	 * MD4方法提供
	 * BC,md4:方法1
	 * @param srcStr
	 * @return
	 */
	public static String bcMd4(String srcStr){
		Digest digest = new MD4Digest();
		digest.update(srcStr.getBytes(), 0, srcStr.length());
		byte[] md4Bytes = new byte[digest.getDigestSize()];
		digest.doFinal(md4Bytes, 0);
		String md4Str = org.bouncycastle.util.encoders.Hex.toHexString(md4Bytes);
		return md4Str;
		
	}
	
	/**
	 * MD4方法提供
	 * BC,md4:方法2,添加配置文件的方法
	 * @param srcStr
	 */
	public static String bcMd42(String srcStr){
		String md4Str =null;
		try {
			Security.addProvider(new BouncyCastleProvider());
			MessageDigest md = MessageDigest.getInstance("MD4");
			byte[] md4Bytes = md.digest(srcStr.getBytes());
			//将byte数组转成16进制；
			md4Str = Hex.encodeHexString(md4Bytes);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return md4Str;
		}
		return md4Str;
	}
 
	/**
	 * BC,md5
	 * @param srcStr
	 * @return
	 */
	public static String bcMd5(String srcStr){
		Digest digest = new MD5Digest();
		digest.update(srcStr.getBytes(), 0, srcStr.length());
		byte[] md5Bytes = new byte[digest.getDigestSize()];
		digest.doFinal(md5Bytes, 0);
		String md5Str = org.bouncycastle.util.encoders.Hex.toHexString(md5Bytes);
		return md5Str;
		
	}

	/**
	 * 简单
	 * CC,MD2
	 * @param srcStr
	 * @return
	 */
	public static String ccMd2(String srcStr){
		String md2Str = DigestUtils.md2Hex(srcStr.getBytes());
		return md2Str;
	}
	
 
	/**
	 * 简单
	 * CC,md5
	 * @param srcStr
	 * @return
	 */
	public static String ccMd5(String srcStr){
		String md2Str = DigestUtils.md5Hex(srcStr.getBytes());
		return md2Str;
	}

}
